package com.springmvc.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.springmvc.demo.model.FamilyMember;
import com.springmvc.demo.model.FamilyProfile;
import com.springmvc.demo.model.RegistryFormOfNcms;
import com.springmvc.demo.service.ProfileService;

@Controller
public class ProfileController {
	@Autowired
	ProfileService profileServiceImpl;
	
	@RequestMapping("/homeProfile")
	public String homeProfile() {
		return "homeProfileManagement";
	}
	
	
	
	/*
	 * 获取家庭档案列表
	 */
	@RequestMapping("/getFamilyProfile")
	@ResponseBody
	public JSONObject getFamilyProfile() {
		JSONObject jsonObject=new JSONObject();
		List<FamilyProfile> list=profileServiceImpl.getProfile();
		FamilyProfile f=(FamilyProfile)list.get(1);
		
		JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(list);
		//System.out.println(f.getHost_name());
		jsonObject.put("data", jsonArray1);
		return jsonObject;
		
	}
	
	/*
	 * 获取单个家庭档案
	 */
	@RequestMapping("/getAFamilyProfile")
	@ResponseBody
	public String getAFamilyProfile() {
		JSONObject jsonObject=new JSONObject();
		FamilyProfile f=profileServiceImpl.getAFamilyProfile(3);
		
		
		System.out.println(f.getId());
		System.out.println(f.getHostName());
		
		return f.getHostName();
		
	}
	
	/*
	 * 添加家庭档案
	 */
	@RequestMapping("/addFamilyProfile")
	@ResponseBody
	public String addFamilyProfile(FamilyProfile familyProfile) {

		return profileServiceImpl.addProfile(familyProfile);
		
	}
	
	
	/*
	 * 添加家庭成员
	 */
	@RequestMapping("/addFamilyMember")
	@ResponseBody
	public String addFamilyMember(FamilyMember familyMember) {

		return profileServiceImpl.addFamilyMember(familyMember);
		
	}
	
	
	/*
	 * 获取家庭成员列表
	 */
	@RequestMapping("/getMemberList")
	@ResponseBody
	public JSONObject getMemberList(String familyNo) {
		JSONObject jsonObject=new JSONObject();
		if(familyNo==null||familyNo.equals(""))
			familyNo="4504210101010048";
		List<FamilyMember> list=profileServiceImpl.getMemberListByFamilyNo(familyNo);
		
		
		JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(list);
		//System.out.println(f.getHost_name());
		jsonObject.put("data", jsonArray1);
		return jsonObject;
		
	}
	
	/*
	 * 获取家庭成员列表
	 */
	@RequestMapping("/getMemberInfo")
	@ResponseBody
	public String getMemberInfo(int id) {
		JSONObject jsonObject=new JSONObject();
		FamilyMember fm=profileServiceImpl.getMemberInfo(id);
		
		String jsonArray1 = JSONArray.toJSONString(fm);
		//System.out.println(f.getHost_name());
		jsonObject.put("data", jsonArray1);
		return jsonArray1;
		
	}
	
	
	@PostMapping("/regNCMS")
	@ResponseBody
	public String regNCMS(RegistryFormOfNcms r,String isReg) {
		
		String msg=profileServiceImpl.regNCMS(r);
		FamilyMember fmMember=new FamilyMember();
		fmMember.setIdCard(r.getIdCard());
		fmMember.setIsReg(isReg);
		profileServiceImpl.updateMember(fmMember);
		return msg;
	}
	
	
}
